export abstract class ResponseBase {
    message: string;
    code: number;
    result: boolean;

    constructor(message: string, code: number, result: boolean) {
        this.message = message;
        this.code = code;
        this.result = result;
    }
}
export class ErrorResponse extends ResponseBase {
    constructor(message: string, code: number) {
        super(message, code, false);
    }

    // 因为在 ErrorResponse 的构造函数中,明确调用了父类 ResponseBase 的构造函数,并固定传入了 result: false.
    static badRequest(message: string, code: number): ErrorResponse {
        return new ErrorResponse(message, code);
    }

    static notFound(message: string, code: number): ErrorResponse {
        return new ErrorResponse(message, code);
    }

    static serverError(message = 'Server error', code: number): ErrorResponse {
        return new ErrorResponse(message, code);
    }
}

export class SuccessResponse<T> extends ResponseBase {
    data: T;
    private constructor(message: string, code: number, data: T) {
        super(message, code, true);
        this.data = data;
    }

    static ok<T>(data: T, message="Succcess"): SuccessResponse<T>{
        return new SuccessResponse<T>(message, 200, data);
    }

    static created<T>(data: T, message="Created"): SuccessResponse<T>{
        return new SuccessResponse<T>(message, 201, data);
    }
}
